package mbalib.ui.managers
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;
	//提示类
	public class TooltipManager extends Object
	{
		private var trigger2Tooltip:Dictionary;
		private var textField:TextField;
		private var _root:DisplayObjectContainer;
		private var container:Sprite;
		private var _defaultRender:Object = null;
		private var timer:Timer;
		
		public function TooltipManager()
		{
			this.trigger2Tooltip = new Dictionary(true);
			this.timer = new Timer(4000, 1);
			this.container = new Sprite();
			this.container.mouseEnabled = false;
			this.container.tabEnabled = false;
			this.container.mouseChildren = false;
			this.textField = new TextField();
			this.textField.background = true;
			this.textField.backgroundColor = 16777164;
			this.textField.border = true;
			this.textField.borderColor = 0;
			this.textField.multiline = false;
			this.textField.wordWrap = false;
			this.textField.autoSize = "left";
			var _loc_1:* = new TextFormat("宋体", 12, 0);
			_loc_1.indent = 2;
			this.textField.setTextFormat(_loc_1);
			this.textField.defaultTextFormat = _loc_1;
			this.timer.addEventListener(TimerEvent.TIMER, this.handleTimer);
			return;
		}// end function
		
		public function get root() : DisplayObjectContainer
		{
			return this._root;
		}// end function
		
		public function set root(param1:DisplayObjectContainer) : void
		{
			if (this._root == null)
			{
				this._root = param1;
				this._root.addChild(this.container);
			}
			else
			{
				trace("Error:root只允许设置一次，之后不允许修改。");
			}
			return;
		}// end function
		
		public function get hideDelay() : Number
		{
			return this.timer.delay;
		}// end function
		
		public function set hideDelay(param1:Number) : void
		{
			this.timer.delay = param1;
			return;
		}// end function
		
		public function get defaultRender()
		{
			return this._defaultRender;
		}// end function
		
		public function set defaultRender(param1) : void
		{
			this._defaultRender = param1;
			return;
		}// end function
		
		private function getRenderInstance(param1)
		{
			var _loc_2:* = undefined;
			if (param1 != null)
			{
				_loc_2 = InstanceManager.createSingletonInstance(param1);
			}
			else if (this.defaultRender != null)
			{
				_loc_2 = InstanceManager.createSingletonInstance(this.defaultRender);
			}
			else
			{
				_loc_2 = this.textField;
			}
			return _loc_2;
		}// end function
		
		private function showTooltip(param1:String, param2, param3:InteractiveObject) : void
		{
			var _loc_6:Number = NaN;
			var _loc_7:Number = NaN;
			var _loc_8:Number = NaN;
			var _loc_9:Number = NaN;
			var _loc_10:Number = NaN;
			var _loc_11:Number = NaN;
			var _loc_4:* = this.getRenderInstance(param2);
			this.container.addChild(_loc_4);
			_loc_4.text = param1;
			var _loc_5:* = this.container.stage;
			if (this.container.stage != null)
			{
				_loc_6 = _loc_5.mouseX;
				_loc_7 = _loc_5.mouseY;
				_loc_8 = _loc_4.width;
				_loc_9 = _loc_4.height;
				_loc_10 = _loc_5.stageWidth;
				_loc_11 = _loc_5.stageHeight;
				if (_loc_6 + _loc_8 + 2 > _loc_10)
				{
					_loc_4.x = this.container.globalToLocal(new Point(_loc_10, _loc_11)).x - _loc_8 - 2;
				}
				else
				{
					_loc_4.x = this.container.mouseX;
				}
				if (_loc_7 + _loc_9 + 22 > _loc_11)
				{
					_loc_4.y = this.container.mouseY - _loc_9;
				}
				else
				{
					_loc_4.y = this.container.mouseY + 22;
				}
			}
			this.timer.reset();
			this.timer.start();
			return;
		}// end function
		
		private function hideTooltip(param1) : void
		{
			var _loc_2:* = this.getRenderInstance(param1);
			if (this.container.contains(_loc_2))
			{
				this.container.removeChild(_loc_2);
			}
			this.timer.reset();
			this.timer.stop();
			return;
		}// end function
		
		public function register(param1:InteractiveObject, param2:String, param3 = null) : void
		{
			if (param2 != null)
			{
				this.trigger2Tooltip[param1] = {text:param2, render:param3};
				param1.addEventListener(MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
				param1.addEventListener(MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
			}
			else
			{
				param1.removeEventListener(MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
				param1.removeEventListener(MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
				delete this.trigger2Tooltip[param1];
			}
			return;
		}// end function
		
		public function unregister(param1:InteractiveObject) : void
		{
			this.register(param1, null);
			return;
		}// end function
		
		private function handleTriggerOver(event:MouseEvent) : void
		{
			var _loc_2:* = event.target as InteractiveObject;
			var _loc_3:* = this.trigger2Tooltip[_loc_2]["text"];
			var _loc_4:* = this.trigger2Tooltip[_loc_2]["render"];
			if (!event.buttonDown)
			{
				this.showTooltip(_loc_3, _loc_4, _loc_2);
			}
			return;
		}// end function
		
		private function handleTriggerOut(event:MouseEvent) : void
		{
			var _loc_2:* = event.target as InteractiveObject;
			var _loc_3:* = this.trigger2Tooltip[_loc_2]["render"];
			this.hideTooltip(_loc_3);
			return;
		}// end function
		
		private function handleTimer(event:TimerEvent) : void
		{
			if (this.container.numChildren)
			{
				this.container.removeChildAt(0);
			}
			return;
		}// end function
		
		public static function getInstance() : TooltipManager
		{
			return InstanceManager.createSingletonInstance(TooltipManager);
		}// end function
		
	}
}
